﻿using System;
using System.Collections.Generic;
using System.Text;
using BingZi.HY.Core.Enums;

namespace BingZi.HY.Core.BLL
{
    /// <summary>
    /// 分页存储过程查询参数类
    /// </summary>
    [Serializable]
    public class QueryParam
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public QueryParam()
            : this(1, int.MaxValue)
        { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mPageIndex">当前页码</param>
        /// <param name="_mPageSize">每页记录数</param>
        public QueryParam(int _mPageIndex, int _mPageSize)
        {
            _PageIndex = _mPageIndex;
            _PageSize = _mPageSize;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mOrderType">排序类型 1:降序 其它为升序</param>
        /// <param name="_mPageIndex">当前页码</param>
        /// <param name="_mPageSize">每页记录数</param>
        public QueryParam(int _mOrderType, int _mPageIndex, int _mPageSize)
        {
            _OrderType = _mOrderType;
            _PageIndex = _mPageIndex;
            _PageSize = _mPageSize;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mWhere">查询条件 需带Where</param>
        /// <param name="_mOrderType">排序类型 1:降序 其它为升序</param>
        /// <param name="_mPageIndex">当前页码</param>
        /// <param name="_mPageSize">每页记录数</param>
        public QueryParam(List<SearchParam> _mWhere, int _mOrderType,
            int _mPageIndex, int _mPageSize)
        {
            _Where = _mWhere;
            _OrderType = _mOrderType;
            _PageIndex = _mPageIndex;
            _PageSize = _mPageSize;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_mWhere">查询条件 需带Where</param>
        /// <param name="_mOrderfld">排序字段</param>
        /// <param name="_mOrderType">排序类型 1:降序 其它为升序</param>
        /// <param name="_mPageIndex">当前页码</param>
        /// <param name="_mPageSize">每页记录数</param>
        public QueryParam(List<SearchParam> _mWhere, string _mOrderfld, int _mOrderType,
            int _mPageIndex, int _mPageSize)
        {
            _Where = _mWhere;
            _Orderfld = _mOrderfld;
            _OrderType = _mOrderType;
            _PageIndex = _mPageIndex;
            _PageSize = _mPageSize;
        }



 



        #region "Private Variables"
        private string _ReturnFields = "*";
        private List<SearchParam> _Where;
        private string _Orderfld = string.Empty;
        private int _OrderType = 1;
        private int _PageIndex = 1;
        private int _PageSize = int.MaxValue;
        private int _startRowIndex = 0;
        private int _RecordCount = int.MaxValue;
        #endregion

        #region "Public Variables"
        /// <summary>
        /// 返回字段
        /// </summary>
        public string ReturnFields
        {
            get
            {
                return _ReturnFields;
            }
            set
            {
                _ReturnFields = value;
            }
        }
        /// <summary>
        /// 查询条件 需带Where
        /// </summary>
        public List<SearchParam> Where
        {
            get
            {
                return _Where;
            }
            set
            {
                _Where = value;
            }
        }





        /// <summary>
        /// 排序字段
        /// </summary>
        public string Orderfld
        {
            get
            {
                return _Orderfld;
            }
            set
            {
                _Orderfld = value;
            }
        }


        /// <summary>
        /// 排序类型 1:降序 其它为升序
        /// </summary>
        public int OrderType
        {
            get
            {
                return _OrderType;
            }
            set
            {
                _OrderType = value;
            }
        }


        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
            }

        }


        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
            }
        }

        /// <summary>
        /// 开始行数
        /// </summary>
        public int startRowIndex
        {
            get
            {
                //第一页时,行集索引从零开始 || 实际的行数不够分一页时
                if (_PageIndex == 1 || _RecordCount <= _PageSize) return _startRowIndex;

                //计算出总页数
                int MaxPage = _RecordCount / _PageSize + (_RecordCount % _PageSize > 0 ? 1 : 0);

                //页数不能超过实际最大页面的判断
                if( _PageIndex > MaxPage ) _PageIndex = MaxPage;

                //起始值
                return (_PageIndex - 1) * _PageSize;
            }
        }
 
        /// <summary>
        /// 结果数量
        /// </summary>
        public int RecordCount
        {
            get
            {
                return _RecordCount;
            }
            set
            {
                _RecordCount = value;
            }
        }
 


        #endregion
    }

    /// <summary>
    /// 查询参数
    /// </summary>
    [Serializable]
    public class SearchParam
    {

        #region "Private Variables"
        private string _FieldName;
        private object _FieldValue;
        private SearchType _searchType = SearchType.Equal;
        #endregion

        #region "Public Variables"


        /// <summary>
        /// 查询字段
        /// </summary>
        public string FieldName
        {
            get
            {
                return _FieldName;
            }
            set
            {
                _FieldName = value;
            }
        }

        /// <summary>
        /// 查询值
        /// </summary>
        public object FieldValue
        {
            get
            {
                return _FieldValue;
            }
            set
            {
                _FieldValue = value;
            }
        }
        /// <summary>
        /// 查询类型
        /// </summary>
        public SearchType searchType
        {
            get
            {
                return _searchType;
            }
            set
            {
                _searchType = value;
            }
        }

        #endregion


        public SearchParam()
        {
 
        }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="__FieldName">查询字段</param>
        /// <param name="__FieldValue">查询值</param>
        /// <param name="__searchType">查询类型</param>
        public SearchParam(string __FieldName, object __FieldValue, SearchType __searchType)
        {
            _FieldName = __FieldName;
            _FieldValue = __FieldValue;
            _searchType = __searchType;
        }



        /// <summary>
        /// 转换成Sql语句
        /// </summary>
        /// <returns>Sql语句</returns>
        public string ToSql()
        {
            if (searchType == SearchType.Like)
                return string.Format(" {0} like '%{1}%' ", _FieldName, _FieldValue);
            else if(searchType == SearchType.Gt)
                return string.Format(" {0} > '{1}' ", _FieldName, _FieldValue);
            else if(searchType == SearchType.Lt)
                return string.Format(" {0} < '{1}' ", _FieldName, _FieldValue);
            else if (searchType == SearchType.Ne)
                return string.Format(" {0} <> '{1}' ", _FieldName, _FieldValue);
            else  
                return string.Format(" {0} = '{1}' ", _FieldName, _FieldValue);
        }
   
    }



}
